﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using nvn.Core.MVP;
using Hutech.CRM.Library.Models.Entities;

namespace Hutech.CRM.Library.Models.DataAccessLayer
{
    public class CriterionCustomerDAL
    {

        /// <summary>
        /// Inserts a row in the cs_CriterionForGroupCustomer table. Returns rows affected count.
        /// </summary>
        /// <param name="criterionGuid"> criterionGuid </param>
        /// <param name="criterionName"> criterionName </param>
        /// <param name="groupLevelCustomerGuid"> groupLevelCustomerGuid </param>
        /// <param name="groupLevelCustomerName"> groupLevelCustomerName </param>
        /// <param name="priority"> priority </param>
        /// <param name="isActive"> isActive </param>
        /// <param name="lastUpdatedDate"> lastUpdatedDate </param>
        /// <param name="uRLIcon"> uRLIcon </param>
        /// <returns>int</returns>
        public static int Create(CriterionCustomer item)
        {
            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetWriteConnectionString(), "cs_CriterionForGroupCustomer_Insert", 8);
            sph.DefineSqlParameter("@CriterionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, item.CriterionGuid);
            sph.DefineSqlParameter("@CriterionName", SqlDbType.NVarChar, 256, ParameterDirection.Input, item.CriterionName);
            sph.DefineSqlParameter("@GroupLevelCustomerGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, item.GroupLevelCustomerGuid);
            sph.DefineSqlParameter("@GroupLevelCustomerName", SqlDbType.NVarChar, 256, ParameterDirection.Input, item.GroupLevelCustomerName);
            sph.DefineSqlParameter("@Priority", SqlDbType.Int, ParameterDirection.Input, item.Priority);
            sph.DefineSqlParameter("@IsActive", SqlDbType.Bit, ParameterDirection.Input, item.IsActive);
            sph.DefineSqlParameter("@LastUpdatedDate", SqlDbType.DateTime, ParameterDirection.Input, item.LastUpdatedDate);
            sph.DefineSqlParameter("@URLIcon", SqlDbType.NVarChar, 256, ParameterDirection.Input, item.URLIcon);
            int rowsAffected = sph.ExecuteNonQuery();
            return rowsAffected;

        }


        /// <summary>
        /// Updates a row in the cs_CriterionForGroupCustomer table. Returns true if row updated.
        /// </summary>
        /// <param name="criterionGuid"> criterionGuid </param>
        /// <param name="criterionName"> criterionName </param>
        /// <param name="groupLevelCustomerGuid"> groupLevelCustomerGuid </param>
        /// <param name="groupLevelCustomerName"> groupLevelCustomerName </param>
        /// <param name="priority"> priority </param>
        /// <param name="isActive"> isActive </param>
        /// <param name="lastUpdatedDate"> lastUpdatedDate </param>
        /// <param name="uRLIcon"> uRLIcon </param>
        /// <returns>bool</returns>
        public static bool Update(CriterionCustomer item)
        {
            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetWriteConnectionString(), "cs_CriterionForGroupCustomer_Update", 8);
            sph.DefineSqlParameter("@CriterionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, item.CriterionGuid);
            sph.DefineSqlParameter("@CriterionName", SqlDbType.NVarChar, 256, ParameterDirection.Input, item.CriterionName);
            sph.DefineSqlParameter("@GroupLevelCustomerGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, item.GroupLevelCustomerGuid);
            sph.DefineSqlParameter("@GroupLevelCustomerName", SqlDbType.NVarChar, 256, ParameterDirection.Input, item.GroupLevelCustomerName);
            sph.DefineSqlParameter("@Priority", SqlDbType.Int, ParameterDirection.Input, item.Priority);
            sph.DefineSqlParameter("@IsActive", SqlDbType.Bit, ParameterDirection.Input, item.IsActive);
            sph.DefineSqlParameter("@LastUpdatedDate", SqlDbType.DateTime, ParameterDirection.Input, item.LastUpdatedDate);
            sph.DefineSqlParameter("@URLIcon", SqlDbType.NVarChar, 256, ParameterDirection.Input, item.URLIcon);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Deletes a row from the cs_CriterionForGroupCustomer table. Returns true if row deleted.
        /// </summary>
        /// <param name="criterionGuid"> criterionGuid </param>
        /// <returns>bool</returns>
        public static bool Delete(
            Guid criterionGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetWriteConnectionString(), "cs_CriterionForGroupCustomer_Delete", 1);
            sph.DefineSqlParameter("@CriterionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, criterionGuid);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Gets an IDataReader with one row from the cs_CriterionForGroupCustomer table.
        /// </summary>
        /// <param name="criterionGuid"> criterionGuid </param>
        public static IDataReader GetOne(
            Guid criterionGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetReadConnectionString(), "cs_CriterionForGroupCustomer_SelectOne", 1);
            sph.DefineSqlParameter("@CriterionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, criterionGuid);
            return sph.ExecuteReader();

        }

        /// <summary>
        /// Gets a count of rows in the cs_CriterionForGroupCustomer table.
        /// </summary>
        public static int GetCount()
        {

            return Convert.ToInt32(SqlHelper.ExecuteScalar(
                ConnectionStringStatic.GetReadConnectionString(),
                CommandType.StoredProcedure,
                "cs_CriterionForGroupCustomer_GetCount",
                null));

        }

        /// <summary>
        /// Gets an IDataReader with all rows in the cs_CriterionForGroupCustomer table.
        /// </summary>
        public static IDataReader GetAll()
        {

            return SqlHelper.ExecuteReader(
                ConnectionStringStatic.GetReadConnectionString(),
                CommandType.StoredProcedure,
                "cs_CriterionForGroupCustomer_SelectAll",
                null);

        }

        /// <summary>
        /// Gets a page of data from the cs_CriterionForGroupCustomer table.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static IDataReader GetPage(
            int pageNumber,
            int pageSize,
            out int totalPages)
        {
            totalPages = 1;
            int totalRows
                = GetCount();

            if (pageSize > 0) totalPages = totalRows / pageSize;

            if (totalRows <= pageSize)
            {
                totalPages = 1;
            }
            else
            {
                int remainder;
                Math.DivRem(totalRows, pageSize, out remainder);
                if (remainder > 0)
                {
                    totalPages += 1;
                }
            }

            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetReadConnectionString(), "cs_CriterionForGroupCustomer_SelectPage", 2);
            sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber);
            sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize);
            return sph.ExecuteReader();

        }

        #region Add by tsandtm

        internal static IDataReader GetAllGroup(Guid GroupCustomerGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetReadConnectionString(), "cs_CriterionForGroupCustomer_GetAllBycroupCustomerGuid", 1);
            sph.DefineSqlParameter("@GroupLevelCustomerGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, GroupCustomerGuid);
            return sph.ExecuteReader();
        }
        #endregion
    }

}



